{include file="header.html"}
<body>

<div class="navBar">
	<a class="left" href="javascript:history.back()"><i class="return"></i></a>
	<div class="titleView-x">我要提现</div>
</div>

<div class="withdraw-index main-top">
	<ul class="tableView tableView-noMargin tableView-light">
		<li>
			<a href="javascript:void(0)">
				<h1><big>请选择提现账户</big>到账银行卡</h1>
			</a>
		</li>
	</ul>
	<span class="title">请输入提现金额</span>
	<form action="api.php?app=withdraw&act=apply" method="post">
	<input type="hidden" name="gourl" value="wap.php?app=withdraw&act=complete" />
	<input type="hidden" name="bank_id" id="bank_id" value="" />
	<div class="withdraw_money">￥<input type="tel" name="withdraw_money" id="withdraw_money"/></div>
	</form>
	
	<font>当前可提现金额为{number_format($member->commission, 2, '.', '')}元</font>
	
	<div class="buttonView">
		<a class="btn pass" href="javascript:void(0)">我要提现</a><!--?app=withdraw&act=complete-->
	</div>
	
	<font class="memo scale10-left">提现金额最低为100元</font>
	
	<div class="withdraw-dialog" style="display:none;">
		<div class="d-title"><a class="error-ico" href="javascript:void(0)"></a><strong>请输入提现密码</strong></div>
		<span>提现</span>
		<font>￥0</font>
		<input type="tel" id="password" />
	</div>
</div>

<input type="hidden" id="member_withdraw_password" value="{$member->withdraw_password}" />
{include file="footer.html"}
<script>
var commission = Number('{$member->commission}'), bank = { };
$(function(){
    $('#password').passwordView(function(){
		$.postJSON('api.php?app=withdraw&act=password', { password:this.val() }, function(json){
			if(json.error!=0){ $.overloadError(json.msg);return }
			$.overlay(false);
			$('#gourl').val('wap.php?app=withdraw&act=complete&bank_name='+bank.bank_name+'&card_number='+bank.card_number.right(4)+'&withdraw_money='+$('#withdraw_money').val());
			$('form').submit();
		});
    });
	$('#withdraw_money').placeholder('0');
	$('.withdraw-index .tableView a').click(function(){
		getBank(function(json){
			var faceView = bindFaceView(null, '.bank-mybank');
			faceView.find('.tableView a').click(function(){
				var i = $(this).parent().index(), g = json.data[i];
				bank = g;
				g.withdraw_money = $('#withdraw_money').val();
				$('#bank_id').val(g.id);
				$('.withdraw-index .tableView a big').addClass('x').html(g.bank_name+' 尾号'+g.card_number.right(4)+' '+g.name);
				removeFaceView();
			});
			faceView.find('.navBar .right').click(function(){
				addBank(function(){
					var faceView = bindFaceView();
					$('#card_number').creditCard();
					faceView.find('.buttonView a').click(function(){
						if (!$('#bank_name').val().length || !$('#card_number').val().length || !$('#branch_name').val().length || !$('#name').val().length) {
							$.overloadError('所有项都必须填写');
							return;
						}
						$.postJSON('api.php?app=bank&act=add', { bank_name:$('#bank_name').val(), card_number:$('#card_number').val().replace(/\s/g, ''), branch_name:$('#branch_name').val(), name:$('#name').val() }, function(json){
							if(json.error!=0){ $.overloadError(json.msg);return }
							var g = json.data;
							$('#bank_id').val(g.id);
							$('.withdraw-index .tableView a big').addClass('x').html(g.bank_name+' 尾号'+g.card_number.right(4)+' '+g.name);
							removeAllFaceView();
						});
					});
				});
			});
		});
	});
	$('.btn').click(function(){
		if(!$('#withdraw_money').val().length){
			$.overloadError('请输入提现金额');
			return;
		}
		if ($('#withdraw_money').val()<=0 || $('#withdraw_money').val()>commission) {
			$.overloadError('提现金额不合法');
			return;
		}
		if ($('#withdraw_money').val()<100) {
			$.overloadError('提现金额最低为100元');
			return;
		}
		if(!$('#bank_id').val().length){
			$.overloadError('请选择提现账户');
			return;
		}
		$('.withdraw-dialog font').html('￥'+$('#withdraw_money').val());
		if(!$('#member_withdraw_password').val().length){
			alert('请先设置提现密码');
			location.href = 'wap.php?app=bank&act=password_set&url='+$.urlencode(location.href);
			return;
		}
		$.overlay('.withdraw-dialog', 0, function(){
			$('#password').select().focus();
		});
	});
});

function getBank(fn){
	$.getJSON('api.php?app=bank&act=index&pagesize=1000', function(json){
		if(json.error!=0){ $.overloadError(json.msg);return }
		var html = '<div class="faceView gr">\
			<div class="navBar">\
				<a class="left" href="javascript:history.back()"><i class="return"></i></a>\
				<div class="titleView-x">银行卡列表</div>\
				<a class="right" href="javascript:void(0)"><span>添加</span></a>\
			</div>\
			<div class="bank-mybank main-padding-top">\
				<ul class="list tableView tableView-noMargin tableView-light">';
					if($.isArray(json.data))for(var i=0; i<json.data.length; i++){
					html += '<li>\
							<a href="javascript:void(0)">\
								<h1>\
									<big>'+json.data[i].bank_name+' 尾号'+json.data[i].card_number.right(4)+' '+json.data[i].name+'</big>\
									银行卡\
								</h1>\
							</a>\
						</li>';
					}
				html += '</ul>\
			</div>\
		</div>';
		$('body').append(html);
		fn(json);
	});
}

function addBank(fn){
	var html = '<div class="faceView gr">\
		<div class="navBar">\
			<a class="left" href="javascript:history.back()"><i class="return"></i></a>\
			<div class="titleView-x">添加提现账户</div>\
		</div>\
		<div class="bank-add main-top">\
			<ul class="tableView tableView-noMargin tableView-light">\
				<li>\
					<h1>\
						<div><span>银行</span><input type="text" name="bank_name" id="bank_name" placeholder="请输入银行名称" /></div>\
					</h1>\
				</li>\
				<li>\
					<h1>\
						<div><span>银行卡卡号</span><input type="tel" name="card_number" id="card_number" placeholder="请输入银行卡卡号" /></div>\
					</h1>\
				</li>\
				<li>\
					<h1>\
						<div><span>开户支行</span><input type="text" name="branch_name" id="branch_name" placeholder="请输入开户支行" /></div>\
					</h1>\
				</li>\
				<li>\
					<h1>\
						<div><span>名称</span><input type="text" name="name" id="name" placeholder="请输入名称" /></div>\
					</h1>\
				</li>\
			</ul>\
			<div class="buttonView">\
				<a class="btn pass" href="javascript:void(0)">确定</a>\
			</div>\
		</div>\
	</div>';
	$('body').append(html);
	fn();
}
</script>